package com.crm.service.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import com.crm.dao.BizConfigMapper;
import com.crm.model.BizConfig;
import com.crm.service.BizConfigService;
import com.crm.util.JsonUtil;
import com.crm.util.UUIDUtil;

@Service
public class BizConfigServiceImpl extends BaseServiceImpl<BizConfig> implements BizConfigService{
	
	@Autowired
	BizConfigMapper bizConfigMapper;
	
	@Override
	@Transactional
	public void updateConfig(MultiValueMap<String, String> params) {
		
		List<String> configId =	(List<String>)params.get("configId");
		List<String> checked = (List<String>)params.get("checked");
		
		if(!checked.isEmpty())
			bizConfigMapper.updateCheked(checked, Byte.valueOf("1"));
		
		configId.removeAll(checked);
		
		if(!configId.isEmpty())
			bizConfigMapper.updateCheked(configId, Byte.valueOf("0"));
		
		for(String key : params.keySet()){
			
			if(!key.equals("configId") && !key.equals("checked")){
				
				bizConfigMapper.updateValueByPrimaryKey(JsonUtil.toJsonStr(params.get(key)), key);
			}
			
		}
		
		
	}

	@Override
	public String insertReturn(BizConfig record) {
		
		String uuid = UUIDUtil.getUUID();
		record.setConfigId(uuid);
		
		this.insert(record);
		
		return uuid;
	}

	@Override
	public List<BizConfig> selectUsByPrimaryKey(String id) {
		// TODO Auto-generated method stub
		return bizConfigMapper.selectUsByPrimaryKey(id);
	}

}
